<?php

class FullRoutingController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','ChangeMark','ChooseServer'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create'),
				'users'=>array('@'),
			),
// 			array('allow', // allow admin user to perform 'admin' and 'delete' actions
// 				'actions'=>array('admin','delete'),
// 				'users'=>array('@'),
// 			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
		public function actionCreate()
	{
	if(getenv('HTTP_CLIENT_IP')) { 
		$onlineip = getenv('HTTP_CLIENT_IP');
		} elseif(getenv('HTTP_X_FORWARDED_FOR')) { 
		$onlineip = getenv('HTTP_X_FORWARDED_FOR');
		} elseif(getenv('REMOTE_ADDR')) { 
		$onlineip = getenv('REMOTE_ADDR');
		} else { 
		$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
		}
		$user_id=Yii::app()->user->id;
		$sql="select * from zy_full_routing where user_id=".Yii::app()->user->id;
		$result = Yii::app()->db->createCommand($sql)->queryAll();
		if(!empty($result)){
			$model=$this->loadModel($result[0]['full_routing_id']);
		}
		else{
			$model=new FullRouting;
		}
		if(isset($_POST['FullRouting'])){
			$time=date('Y-m-d H:i:s');
			$model->attributes=$_POST['FullRouting'];
			//开通全路由
			if($_POST['full']=="on"&&$model->vpnlist_id>0){
				$model->mark=1;
				$model->open_time=$time;
			}
			//关闭全路由
			if(isset($_POST['FullRouting']['full_routing_close'])){
				$model->mark=0;
				$model->vpnlist_id=0;
				$model->open_time="0000-00-00 00:00:00";
			}
			//开启智能路由
			if($_POST['smart']=="on"){
				$model->smart_mark=1;
			}
			//关闭智能路由
			if(isset($_POST['FullRouting']['smart_mark_close'])){
				$model->smart_mark=0;
			}
			$model->user_id=Yii::app()->user->id;
			if($model->full_routing_id>0){
				$model->update_time=$time;
			}else{
				$model->create_time=$time;
			}
			
			if($_POST['full']=="on"||$_POST['smart']=="on"){
				$operate_content.=$_POST['full']=="on"?"Open Full Routing":"";
				$operate_content.=$_POST['smart']=="on"?"Open Smart Routing":"";
				$sql="insert into zy_record(user_id,IP,SN,operate_content,create_time) values($user_id,'$onlineip','PC Client','$operate_content','$time')";
				$connect=yii::app()->db;
				$command=$connect->createCommand($sql);
				$command->execute();
			}
			$model->save();
			
			$UpdateModel = new Updatestatus;
			$isset = $UpdateModel->findByPk(yii::app()->user->id);
			$level = yii::app()->user->level();
			//开启全路由
			if($_POST['full']=="on"&&$model->vpnlist_id>0){
				if(empty($isset)){
					$UpdateModel->user_id=yii::app()->user->id;
					$UpdateModel->routeupdate=1;
					$UpdateModel->crate_time = date('Y-m-d h:i:s');
					$UpdateModel->crate_user=yii::app()->user->name;
					$UpdateModel->save();
				}else{
					$UpdateModel->updateByPk(yii::app()->user->id,array('routeupdate'=>1,'update_time'=>date('Y-m-d h:i:s'),'update_user'=>yii::app()->user->name));
				}
			}
			//关闭全路由
			if(isset($_POST['FullRouting']['full_routing_close'])){
				if(empty($isset)){
					$UpdateModel->user_id=yii::app()->user->id;
					$UpdateModel->routeupdate=1;
					$UpdateModel->crate_time = date('Y-m-d h:i:s');
					$UpdateModel->crate_user=yii::app()->user->name;
					$UpdateModel->save();
				}else{
					$UpdateModel->updateByPk(yii::app()->user->id,array('routeupdate'=>1,'update_time'=>date('Y-m-d h:i:s'),'update_user'=>yii::app()->user->name));
				}
			}
			
		}
	   	$UpdateModel = new Updatestatus;
		$isset = $UpdateModel->findByPk(yii::app()->user->id);
		$date_min = date("Y-m-d H:i:s", mktime(date("H") - 2, date("i"), date("s"), date("m"), date("d"), date("Y")));
			
		 if ($date_min > $model->open_time) //超过两个小时
		{
				
			if(empty($isset)){
				$UpdateModel->user_id=yii::app()->user->id;
				$UpdateModel->routeupdate=1;
				$UpdateModel->crate_time = date('Y-m-d h:i:s');
				$UpdateModel->crate_user=yii::app()->user->name;
				$UpdateModel->save();
			}else{
				$UpdateModel->updateByPk(yii::app()->user->id,array('routeupdate'=>1,'update_time'=>date('Y-m-d h:i:s'),'update_user'=>yii::app()->user->name));
			}
		}  
			
			$this->render('create',array('model'=>$model,));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['FullRouting']))
		{
			$model->attributes=$_POST['FullRouting'];
			$model->attributes->vpnlist_id=0;
			var_dump($model->attributes);die();
			//$model->open_time=date('0000-00-00 00:00:00');
			//$model->mark=1;
			if($model->save())
				$this->redirect(array('create'));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('FullRouting');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new FullRouting('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['FullRouting']))
			$model->attributes=$_GET['FullRouting'];
	
		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return FullRouting the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=FullRouting::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param FullRouting $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='full-routing-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	/**
	 * 更新mark字段内容，并返回当前页
	 */
	public function actionChangeMark(){
		$id = $_GET['full_routing_id'];
		$mark = $_GET['mark'];
		FullRouting::model()->changeMark($id,$mark);
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	/**
	 * 选择开启全路由服务器
	 */
	public function  actionChooseServer($id){
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['FullRouting']))
		{
			$model->attributes=$_POST['FullRouting'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->full_routing_id));
		}

		$this->render('chooseserver',array(
			'model'=>$model,
		));
	}
	
}
